package woa.stack;

import java.util.Arrays;

/**
 * 顺序栈
 * 数组实现，只支持固定长度，如需支持变长，则需要自动扩容
 *
 * @author wangpeng
 * @date 2021/11/9
 */
public class ArrayStack implements WoaStack {

    private int[] arr;
    private int count = 0;

    public ArrayStack(int len) {
        this.arr = new int[len];
    }

    /**
     * 入栈
     *
     * @param item
     */
    @Override
    public boolean push(int item) {
        if (count == arr.length) {
            return false;
        }
        arr[count] = item;
        count++;
        return true;
    }

    @Override
    public int pop() {
        if (count == 0) {
            return -1;
        }
        count--;
        return arr[count];
    }

    @Override
    public String toString() {
        return Arrays.toString(arr);
    }
}
